document.ready(function() {
    setValue('otherid', getUrlParam('otherid'));

    openLoad();

    var param = {};
    param.ctrl = 'other';
    param.func = 'getOtherInfo';
    param.otherid = getUrlParam('otherid');
    param.flag = 'edit';

    sendAjax(param, function(result) {
        var info = result.otherInfo;

        setValue('title', info.title);
        setValue('address', info.address);
        setValue('position', info.position);
        setValue('person', info.person);
        setValue('phone', info.phone);
        setValue('remark', info.remark);
        setValue('feature', info.feature);
        setValue('area', info.area);
        setValue('introduce', info.introduce);

        setSelectItem('sortation', result.sortationList);

        locationSelect('sortation', info.sortation);

        if (info.smallpic != null && info.smallpic != '') {
            files = info.smallpic;
            getItem('smallPic').src = info.smallpic;
        }

        var picHtml = '';
        var picList = info.picList;
        for (var i = 0; i < picList.length; i++) {
            fileList[picList[i].name] = picList[i].path;

            picHtml += '<div class="upload-img" style="background-image: url(\'' + picList[i].path + '\');">';
            picHtml += '<img src="static/pic/pc/delete.png" class="button4" onclick="deletePic(this, \'' + picList[i].name + '\')" />';
            picHtml += '</div>';
        }
        fileNum = picList.length;
        if (fileNum >= 6) {
            setDisplay('selectImg2', 'none');
        }
        setHtml('uploadImgBox', picHtml);

        var idList = ['area'];
        var valueList = [info.area];
        for (var m = 0; m < idList.length; m++) {
            var objList = getItem(idList[m]).parentNode.children;
            for (var i = 0; i < objList.length; i++) {
                if (objList[i].className == 'tag-item') {
                    if (valueList[m].includes(objList[i].innerHTML)) {
                        objList[i].className = 'tag-item-selected';
                    }
                }
            }
        }

        setDisplay('mainBg', '');
        closeLoad();
    });
});

function toMap() {
    window.open("https://lbs.qq.com/tool/getpoint/");
}

function back() {
    parent.closeSlipIframeDiv();
}

function selectTag(obj, id) {
    var text = obj.innerText;
    if (obj.className == 'tag-item-selected') {
        obj.className = 'tag-item';
        if (getValue(id).includes(text)) {
            setValue(id, getValue(id).replace('，' + text, ''));
            setValue(id, getValue(id).replace(',' + text, ''));
            setValue(id, getValue(id).replace(text + '，', ''));
            setValue(id, getValue(id).replace(text + ',', ''));
            setValue(id, getValue(id).replace(text, ''));
        }
    }
    else {
        obj.className = 'tag-item-selected';
        if (!getValue(id).includes(text)) {
            if (getValue(id) != '') {
                setValue(id, getValue(id) + '，' + text);
            }
            else {
                setValue(id, text);
            }
        }
    }
}

function saveOther() {
    if (isEmpty('title') || isWicked('title')) {
        showTip('标题格式异常');
        return;
    }
    if (isEmpty('sortation')) {
        showTip('请选择栏目分类');
        return;
    }
    if (!isEmpty('address') && isWicked('address')) {
        showTip('商家地址格式异常');
        return;
    }
    if (isEmpty('person') || isWicked('person')) {
        showTip('联系人格式异常');
        return;
    }
    if (isEmpty('phone') || (!isMobile('phone') && !isTelphone('phone'))) {
        showTip('联系电话格式异常');
        return;
    }
    if (!isEmpty('remark') && isWicked('remark')) {
        showTip('备注格式异常');
        return;
    }
    if (!isEmpty('feature') && isWicked('feature')) {
        showTip('服务特征格式异常');
        return;
    }
    if (isEmpty('introduce')) {
        showTip('请填写服务详情');
        return;
    }

    var json = [];
    json.push(fileList);

    var param = {};
    param.ctrl = 'other';
    param.func = 'saveOther';
    param.flag = 'edit';
    param.otherid = getAjaxValue('otherid');
    param.title = getAjaxValue('title');
    param.sortation = getAjaxValue('sortation');
    param.address = getAjaxValue('address');
    param.position = getAjaxValue('position');
    param.person = getAjaxValue('person');
    param.phone = getAjaxValue('phone');
    param.feature = getAjaxValue('feature');
    param.area = getAjaxValue('area');
    param.introduce = getAjaxValue('introduce');
    param.remark = getAjaxValue('remark');
    param.smallpic = encodeURIComponent(files);
    param.bigpic = JSON.stringify(json);
    
    sendAjax(param, function(result) {
        submitRecord('edit', '其他服务编辑页', '修改', result.otherid);
        parent.closeSlipIframeDiv();
        parent.queryKey('page');
    });
}